package com.easy.easy.Controller;

import com.easy.easy.Service.IOrderService;
import com.easy.easy.bean.*;
import com.easy.easy.util.LayData;
import com.easy.easy.util.Page;
import com.easy.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    IOrderService orderService;

//    @RequestMapping("add")
//    @ResponseBody
//    public int add(HttpSession session, Order order){
//        System.out.println("进入add方法");
//
//        User user=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
//
//        System.out.println("当前userid="+user.getUserid());
//        order.setUserid(user.getUserid());
//        return orderService.add(order);
//
//    }


    @RequestMapping("getorderbyuserid")
    @ResponseBody
    public LayData getOrderByUserId(HttpSession session) {
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid = loginUser.getUserid();
        List<Order> orders = orderService.getOrderByUserId(userid);
        return LayData.success(orders);
    }

    @RequestMapping("List")
    @ResponseBody
    public LayData getList(Page page, String text, HttpSession session) {
        System.out.println("进入orderlist方法");
        User user = (User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);

        // 检查用户是否登录
        if (user == null) {
            return new LayData(1, "用户未登录", 0, null);
        }

        // 确保page对象不为null，并且有合理的默认值
        if (page == null) {
            page = new Page();
            page.setLimit(10); // 设置默认每页显示10条
        }

        System.out.println("分页参数: start=" + page.getStart() + ", limit=" + page.getLimit());

        List<Order> list = orderService.getList(page, text, user.getUserid());
        int count = orderService.getCount(text, user.getUserid());
        System.out.println("user的值为"+user.getUserid());
        System.out.println("查询到订单数量: " + count);
        System.out.println("返回订单列表大小: " + (list != null ? list.size() : 0));

        return new LayData(0, "获取成功", count, list);
    }

    @RequestMapping("list")
    @ResponseBody
    public LayData getlist(Page page,String text){
        List<Order> list = orderService.getlist(page,text);
        int count = orderService.getcount(text);
        return new LayData(count,list);
    }

    @RequestMapping("add")
    @ResponseBody
    public LayData add(HttpSession session, Order order){
        System.out.println("进入add方法");

        User user=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);

        System.out.println("当前userid="+user.getUserid());
        order.setUserid(user.getUserid());
        orderService.add(order);
        System.out.println(order.getOrderid());
        return LayData.success(200,"添加成功",order.getOrderid());
    }

    @RequestMapping("payment")
    @ResponseBody
    public LayData payment(int orderid) {
        String code=orderService.payment(orderid);
        return LayData.success(200,"",code);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int delete(int orderid, int userid){
        return orderService.delete(orderid,userid);
    }

    @RequestMapping("getproduct")
    @ResponseBody
    public int getproduct(int orderid){
        return orderService.getproduct(orderid);
    }


    @RequestMapping("paymentsuccess")
    @ResponseBody
    public LayData paymentSuccess(@RequestParam HashMap map){
        System.out.println(map);
        orderService.paymentSuccess(map);
        return LayData.success(200,"");
    }


    @RequestMapping("delivery")
    @ResponseBody
    public int delivery(int orderid){
        return orderService.delivery(orderid);
    }

    @RequestMapping("Delete")
    @ResponseBody
    public int Delete(int orderid){
        return orderService.Delete(orderid);
    }
}
